package com.wondream.myframework.app.common.apollo;

import com.ctrip.framework.apollo.spring.config.ConfigPropertySource;
import com.ctrip.framework.apollo.spring.config.ConfigPropertySourceFactory;
import com.ctrip.framework.apollo.spring.util.SpringInjector;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;

import javax.annotation.Resource;
import java.util.List;

/**
 * apollo启动类，监听所有namespace CfssAutoUpdateConfigChangeListener
 */
public class CfssApolloStartup implements ApplicationRunner {

    private ConfigPropertySourceFactory configPropertySourceFactory = SpringInjector.getInstance(ConfigPropertySourceFactory.class);

    @Resource
    ApplicationContext applicationContext;

    /**
     * 添加应用apollo所有namespace的监听
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<ConfigPropertySource> allConfigPropertySources = configPropertySourceFactory.getAllConfigPropertySources();
        allConfigPropertySources.forEach(source -> {
            source.addChangeListener(new CfssAutoUpdateConfigChangeListener(applicationContext.getEnvironment()));
        });
    }
}
